//
//  UpdateTelCell.m
//  HTHappySale
//
//  Created by 创维海通 on 2017/9/26.
//  Copyright © 2017年 zxyywy. All rights reserved.
//

#import "UpdateTelCell.h"

@interface UpdateTelCell()<UITextFieldDelegate>
{
    NSArray *characterArr;
    
    int countDown;                  //倒计时
}
@property (strong, nonatomic) IBOutlet UITextField *customText;
@property (strong, nonatomic) IBOutlet UIButton *verifyBtn;
@property (strong, nonatomic) dispatch_source_t timer;

@property (nonatomic, strong) CADisplayLink *displayLink;
- (IBAction)verifyAction:(id)sender;

@end

@implementation UpdateTelCell

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
    characterArr = @[@"请输入您的手机号",@"请输入验证码"];
    
    countDown = 60;
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

- (void)configureCell:(int)index
{
    _customText.placeholder = characterArr[index];
    _customText.tag = 200 + index;
    if(index == 0){
        _verifyBtn.hidden = YES;
    }else{
        _verifyBtn.hidden = NO;
    }
}

- (IBAction)verifyAction:(id)sender {
    countDown = 60;
    self.displayLink = [CADisplayLink displayLinkWithTarget:self selector:@selector(timerAction)];
    [self.displayLink setFrameInterval:60];
    [self.displayLink addToRunLoop:[NSRunLoop currentRunLoop] forMode:NSRunLoopCommonModes];
    if ([_telDelegate respondsToSelector:@selector(sendTel)]) {
        [_telDelegate performSelector:@selector(sendTel) withObject:self];
    }
}

- (void)timerAction
{
    if (countDown <= 0) {
        [_verifyBtn setTitle:@"获取验证码" forState:UIControlStateNormal];
        _verifyBtn.enabled = YES;
        [self stopDisplayLink];
    }else
    {
        NSString *timeStr = [NSString stringWithFormat:@"%ds后重发",countDown];
        [_verifyBtn setTitle:timeStr forState:UIControlStateDisabled];
        _verifyBtn.enabled = NO;
        countDown--;
    }
}

- (void)addCountDown
{
    //全局并发队列
    dispatch_queue_t globalQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
    //创建队列
    dispatch_queue_t queue = dispatch_get_global_queue(0, 0);
    
    //创建一个定时器
    _timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, globalQueue);
    dispatch_time_t interval = 1 * NSEC_PER_SEC;
    dispatch_source_set_timer(_timer, DISPATCH_TIME_NOW, interval, 0);
    //设置回调

    dispatch_source_set_event_handler(_timer, ^{
        NSLog(@"Event Handler");
        if (countDown <= 0) {
            dispatch_async(queue, ^{
                [_verifyBtn setTitle:@"获取验证码" forState:UIControlStateNormal];
                _verifyBtn.enabled = YES;
            });
            
        }else{
            countDown--;
            dispatch_async(queue, ^{
                NSString *timeStr = [NSString stringWithFormat:@"%ds后重发",countDown];
                _verifyBtn.enabled = NO;

                [_verifyBtn setTitle:timeStr forState:UIControlStateNormal];
            });
        }
    });
    
    dispatch_source_set_cancel_handler(_timer, ^{ //计时器取消处理器；调用 dispatch_source_cancel 时执行
         NSLog(@"Cancel Handler");
    });
    
    //启动定时器
    dispatch_resume(_timer);
}

- (void)updateButton
{
    if (countDown <= 0) {
        [_verifyBtn setTitle:@"获取验证码" forState:UIControlStateNormal];
        _verifyBtn.enabled = YES;
        [self stopTimer];
    }else
    {
        NSString *timeStr = [NSString stringWithFormat:@"%ds后重发",countDown];
        [_verifyBtn setTitle:timeStr forState:UIControlStateNormal];
        _verifyBtn.enabled = NO;
    }
    
   
}

- (void)stopDisplayLink
{
    [self.displayLink invalidate];
    self.displayLink = nil;
}

- (void)stopTimer
{
    if(_timer){
        dispatch_source_cancel(_timer);
        _timer = nil;
    }
}

-(void) pauseTimer{
    if(_timer){
        dispatch_suspend(_timer);
    }
}
-(void) resumeTimer{
    if(_timer){
        dispatch_resume(_timer);
    }
}


#pragma mark -textFieldDelegate
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    [textField resignFirstResponder];
    return YES;
}

//- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
//{
//    [self.contentView endEditing:YES];
//}
@end
